<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Video extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model(array('video_model','kategori_model','akun_model','komentar_model'));    
		$this->load->library(array('form_validation','session','upload','image_lib'));    
		$this->load->database();    
		$this->load->helper(array('url','form','string','date')); 
			
		
	}
	
	public function utama()
	{	
		$pesan['berhasil'] = true;
		$this->load->view('halaman_utama', $pesan);
	}
	
	
	public function cekLog()
	{
		if(!$this->session->userdata('isLogin')) { redirect('index.php/home');}
	}
	
	private function _upload_gambar()
	{	
		//============================ start upload gambar =================//
		if($_FILES['gambarvideo']['name'] != "" && ISSET($_FILES['gambarvideo']['name'])){
			unset($config);
			$date = date("ymd");
			$config['upload_path'] = './upload/image';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';
			$config['max_size']	= '500';
			$config['max_width']  = '1024';
			$config['max_height']  = '768';
			$config['overwrite'] = FALSE;
			$config['remove_spaces'] = TRUE;
			$namafilegambar = $date.$_FILES['gambarvideo']['name'];
			$config['file_name'] = $namafilegambar;
			
			$this->upload->initialize($config); 

			if ( ! $this->upload->do_upload('gambarvideo'))
			{
				return $datagambar = array (
							'namagambar' => '',
							'pathgambar' => ''
						);
			}
			else
			{
				$data = $this->upload->data();
				return $datagambar = array (
							'namagambar' => $data['file_name'],
							'pathgambar' => $data['full_path']
						);
			}
		} else {
		
			return $datagambar = array (
							'namagambar' => '',
							'pathgambar' => ''
						);
			
		}
		
	}
	
	
	
	private function _upload_video()
	{	
		$date = date("ymd");
		//============================ start upload video =================//
		$this->cekLog();
		if($_FILES['video']['name'] != "" && ISSET($_FILES['video']['name'])){
			
			unset($config);
			$config['upload_path'] = './upload/video';
			$config['allowed_types'] = 'mp4|flv|3gp|ogg';
			$config['max_size']	= '10000'; //max 10 MB
			$config['overwrite'] = TRUE;
			$config['remove_spaces'] = TRUE;
			$namafilevideo = $date.$_FILES['video']['name'];
			$config['file_name'] = $namafilevideo;
			
			$this->upload->initialize($config); 

			if ( ! $this->upload->do_upload('video'))
			{
				$error = array('error' => $this->upload->display_errors());
				//$this->load->view('halaman_unggah_video', $error);
				return $data = array(
						'namavideo' => '',
						'pathvideo' => ''
						);
			}
				else
			{
					$data = $this->upload->data();
					return $data = array(
							'namavideo' => $data['file_name'],
							'pathvideo' => $data['full_path'],
							'fileext' => $data['file_ext']
							);
			}
		}
			else
		{
				return $data = array(
						'namavideo' => '',
						'pathvideo' => ''
						);
		}
		//============================ end upload video =================//
		
	
	}
	
	//ffmpeg -i file.avi -ar 220050 hasil.flv
	
	public function insert_video()
	{
		$this->cekLog();
		$video = $this->_upload_video();
		$gambar = $this->_upload_gambar();
		//print_r($gambar);
		if($gambar['namagambar'] != '' && $video['namavideo'] != '')	{
		
			$date = date("y-m-d");
			
			$namagambar = $gambar['namagambar'];
			$pathgambar = $gambar['pathgambar'];
			
			$namavideo = $video['namavideo'];
			$pathvideo = $video['pathvideo'];
			$fileext = $video['fileext'];
			
			$id_user = $this->session->userdata('id_user');
			$judul_video = $this->input->post('judulvideo');
			$data_video = array(
				'id_user' => $id_user,
				'judul_video' => $this->input->post('judulvideo'),
				'nama_video' => $namavideo,
				'path_video' => $pathvideo,
				'file_ext' => $fileext,
				'sumber_video' => $this->input->post('sumbervideo'),
				'gambar_video' => $namagambar,
				'path_gambar' => $pathgambar,
				'deskripsi_video' => $this->input->post('deskripsivideo'),
				'info_singkat' => $this->input->post('infosingkat'),
				'tgl_upload' => $date
			);
			
			$this->video_model->tambah_video($data_video);
			$id_video = $this->video_model->get_id_video($judul_video, $namavideo);
			
			$pointer_array = $this->input->post('pointer'); 
			$pointer = array_pop($pointer_array); 
			for ($i=0; $i <= $pointer; $i++){
				$data_kategori = array(
					'id_kategori' => $this->input->post('kategorivideo'.$i),
					'id_video' => $id_video
				);		
				$this->kategori_model->insert_kategori_video($data_kategori);
			}
			$this->session->set_flashdata('upload_sukses', time());
			redirect('index.php/video');
			// $this->load->library('unit_test');
			// $test_name = 'Test Insert Video';
			// $test = $data_video['path_video'];
			// $expected_result = $data_video['path_video'] != NULL;
			// $this->unit->run($test, $expected_result, $test_name);
			// echo $this->unit->report();
			//$this->load->view('halaman_unggah_video', $data_video);
			
		} else {
			$this->session->set_flashdata('upload_gagal', time());
			redirect('index.php/video');
		}
	}
	
	public function index()
	{	
		//$this->output->cache(10);
		$this->cekLog();
		if($this->session->flashdata('upload_sukses')){
			$data['berhasil'] = '<div class="alert alert-success"><a class="close" data-dismiss="alert">&times</a><h5> <span class="glyphicon glyphicon-thumbs-up "></span> Video anda telah berhasil diunggah. </h5> </div>';
		} else if($this->session->flashdata('upload_gagal')) {
			$data['berhasil'] = '<div class="alert alert-danger"><a class="close" data-dismiss="alert">&times</a><h5><span class="glyphicon glyphicon-warning-sign "></span> Maaf video anda gagal diunggah!</h5></div>';
		} else {
			$data['berhasil'] = '';
		}
		
		$username = $this->session->userdata('username');
		$data['kategori'] = $this->kategori_model->get_list_kategori();
		$data['nama_kategori'] = $this->kategori_model->get_all_nama_kategori();
		$data['daerah'] = $this->kategori_model->get_list_daerah();
		$data['histori_video'] = $this->video_model->get_histori_video();
		$this->load->view('halaman_unggah_video', $data, array('error' => 'GAGAL' ));
	}
	
	public function videoku()
	{	
		$data['kategori'] = $this->kategori_model->get_list_kategori();
		$data['nama_kategori'] = $this->kategori_model->get_all_nama_kategori();
		$data['daerah'] = $this->kategori_model->get_list_daerah();
		$id_user = $this->session->userdata('id_user');
		$data['histori_video'] = $this->video_model->get_histori_video();
		$data['videoku'] = $this->video_model->get_video_user($id_user);
		$this->load->view('halaman_videoku', $data);
	}
	
	public function lihat_video($id)
	{	
		$this->lihat_video_baru($id);
		$nama_kategori = $this->kategori_model->get_all_nama_video_kategori($id);
		$datavideo = $this->video_model->detail_video($id);
		foreach($datavideo as $row){
			$id_video = $row->id_video;
		}
		$foto_komentator = '';
		$id_user = $this->session->userdata('id_user');
		$histori_video = $this->video_model->get_histori_video();
		$listkomentar = $this->komentar_model->get_list_komentar($id_video);
		$foto_user = $this->akun_model->foto_user($id_user);
		foreach($foto_user as $row){
			$foto_komentator = $row->nama_foto;
		}
		foreach($datavideo as $row){
			$data = array (
				'id_video' => $row->id_video,
				'nama_user' => $row->nama_lengkap,
				'judul_video' => $row->judul_video,
				'nama_video' => $row->nama_video,
				'path_video' => $row->path_video,
				'file_ext' => $row->file_ext,
				'sumber_video' => $row->sumber_video,
				'gambar_video' => $row->gambar_video,
				'path_gambar' => $row->path_gambar,
				'deskripsi_video' => $row->deskripsi_video,
				'info_singkat' => $row->info_singkat,
				'tgl_upload' => $row->tgl_upload,
				'berhasil' => TRUE,
				'nama_kategori' => $nama_kategori,
				'kategori' => $this->kategori_model->get_list_kategori(),
				'daerah' => $this->kategori_model->get_list_daerah(),
				'histori_video' => $histori_video,
				'listkomentar' => $listkomentar,
				'foto_komentator' => $foto_komentator
			);
		}
		
		if($this->session->flashdata('hapus_komentar_sukses')){
			$data['berhasil'] = '<div class="alert alert-success"><a class="close" data-dismiss="alert">&times</a><h5> <span class="glyphicon glyphicon-thumbs-up "></span> Komentar berhasil dihapus. </h5> </div>';
		} else if($this->session->flashdata('hapus_komentar_gagal')) {
			$data['berhasil'] = '<div class="alert alert-danger"><a class="close" data-dismiss="alert">&times</a><h5><span class="glyphicon glyphicon-warning-sign "></span> Maaf komentar gagal dihapus!</h5></div>';
		} else if($this->session->flashdata('validasi_komentar_sukses')){
			$data['berhasil'] = '<div class="alert alert-success"><a class="close" data-dismiss="alert">&times</a><h5> <span class="glyphicon glyphicon-thumbs-up "></span> Validasi komentar berhasil. </h5> </div>';
		} else if($this->session->flashdata('validasi_komentar_gagal')) {
			$data['berhasil'] = '<div class="alert alert-danger"><a class="close" data-dismiss="alert">&times</a><h5><span class="glyphicon glyphicon-warning-sign "></span> Maaf validasi komentar gagal!</h5></div>';
		}  else {
			$data['berhasil'] = '';
		}
		
		$this->load->view('halaman_video', $data);
		return $data;
	}
	
	
	public function data_video()
	{	
		$this->cekLog();
		$id_video = '';
		$data['video'] = $this->video_model->get_daftar_video();
		foreach ($data['video'] as $row) {
			$id_video = $row->id_video;
			$data['kategori'] = $this->kategori_model->get_all_nama_video_kategori($id_video);
			foreach($data['kategori'] as $row){
				
			}
		}
		
		// $this->load->library('pagination');
		// $config['base_url'] = base_url().'index.php/video/data_video/page/';
		// $config['total_rows'] = 5;
		// $config['per_page'] = 1;
		// $config['display_pages'] = TRUE; 
		// $this->pagination->initialize($config);
		// $data['link'] = $this->pagination->create_links();
		
		$data['tidakaktif'] = $this->akun_model->get_banyak_akun_tidak_aktif();
		$data['video_baru'] = $this->video_model->get_banyak_video_baru();
		if($this->session->flashdata('hapus_sukses')){
			$data['berhasil'] = '<div class="alert alert-success"><a class="close" data-dismiss="alert">&times</a><h5> <span class="glyphicon glyphicon-thumbs-up "></span> Video berhasil dihapus. </h5> </div>';
		} else if($this->session->flashdata('hapus_gagal')) {
			$data['berhasil'] = '<div class="alert alert-danger"><a class="close" data-dismiss="alert">&times</a><h5><span class="glyphicon glyphicon-warning-sign "></span> Maaf video gagal dihapus!</h5></div>';
		} else if($this->session->flashdata('validasi_sukses')){
			$data['berhasil'] = '<div class="alert alert-success"><a class="close" data-dismiss="alert">&times</a><h5> <span class="glyphicon glyphicon-thumbs-up "></span> Validasi video berhasil. </h5> </div>';
		} else if($this->session->flashdata('validasi_gagal')) {
			$data['berhasil'] = '<div class="alert alert-danger"><a class="close" data-dismiss="alert">&times</a><h5><span class="glyphicon glyphicon-warning-sign "></span> Maaf validasi video gagal!</h5></div>';
		}  else {
			$data['berhasil'] = '';
		}
		$this->load->view('halaman_master_video', $data);
		return $data;
	}
	
	
	public function delete_video($id_video)
	{
		$this->cekLog();
		if ($this->video_model->hapus_video($id_video) == TRUE) {
			$this->session->set_flashdata('hapus_sukses', time());
			return true;
			redirect('index.php/video/data_video');
		} else {
			$this->session->set_flashdata('hapus_gagal', time());
			redirect('index.php/video/data_video');
			return false;
		}
	}
	
	public function cari_video_sesuai_kategori($id_kategori)
	{	
		$data['kategori'] = $this->kategori_model->get_list_kategori();
		$data['nama_kategori'] = $this->kategori_model->get_all_nama_kategori();
		$data['daerah'] = $this->kategori_model->get_list_daerah();
		$data['video'] = $this->video_model->cari_video_kategori($id_kategori);
		$kosong = count($data['video']);
		if($kosong < 1 ){$pesangagal='Maaf tidak ada video yang terkait. Silahkan memilih kategori lainnya.';} else {$pesangagal = 'Terdapat '.count($data['video']).' video.';}
		$data['kosong'] = $kosong;
		$data['pesangagal'] = $pesangagal;
		$data['histori_video'] = $this->video_model->get_histori_video();
		$this->load->view('halaman_pencarian', $data);
		return $data;
	}
	
	public function cari_video()
	{	
		$data['kategori'] = $this->kategori_model->get_list_kategori();
		$data['nama_kategori'] = $this->kategori_model->get_all_nama_kategori();
		$data['daerah'] = $this->kategori_model->get_list_daerah();
		
		$pencarian = $this->input->post('cari');
		$data['video'] = $this->video_model->get_cari_video($pencarian);
		$kosong = count($data['video']);
		if($kosong < 1 ){$pesangagal='Maaf tidak ada video yang terkait. Silahkan memilih kategori lainnya.';} else {$pesangagal = 'Terdapat '.count($data['video']).' video.';}
		$data['kosong'] = $kosong;
		$data['pesangagal'] = $pesangagal;
		$data['histori_video'] = $this->video_model->get_histori_video();
		$this->load->view('halaman_pencarian', $data);
		return $data;
	}
	
	public function lihat_video_baru($id_video)
	{
		return $this->video_model->update_video_baru($id_video);
		 
	}
	
	public function validasi($id_video)
	{
		$this->cekLog();
		if($this->video_model->validasi_video($id_video) == true){
			$this->session->set_flashdata('validasi_sukses', time());
			//redirect('index.php/video/data_video');
			return true;
		} else	{
			$this->session->set_flashdata('validasi_gagal', time());
			//redirect('index.php/video/data_video');
			return false;
		}
		
	}	
	
	
	// public function unit_test()
	// {	
		// $this->load->library('unit_test');
		// // $test_name = 'Test Insert Video';
		// // $test = $this->insert_video();
		// // $id_user = $this->session->userdata('id_user');
		// // $expected_result = array( $test['id_user'] => $id_user );
		// // $this->unit->run($test, $expected_result, $test_name);
		// // echo $this->unit->report();
		
		// $test_name1 = 'Test videoku()';
		// $test1 = count($this->video_model->get_video_user(1));
		// $expected_result1 = $test1 == 0 || $test1 > 0;
		// $this->unit->run($test1, $expected_result1, $test_name1);
		
		// $test_name2 = 'Test lihat_video()';
		// $test2 = $this->lihat_video('48');
		// $expected_result2 = $test2 != NULL;
		// $this->unit->run($test2, $expected_result2, $test_name2);
		
		// $test_name4 = 'Test cari_video_sesuai_kategori()';
		// $test4 = $this->cari_video_sesuai_kategori(rand());
		// $expected_result4 = $test4 == NULL || $test4 > 0;
		// $this->unit->run($test4, $expected_result4, $test_name4);
		
		// $test_name5 = 'Test cari_video()';
		// $test5 = $this->cari_video(rand());
		// $expected_result5 = $test5 == NULL || $test5 > 0;
		// $this->unit->run($test5, $expected_result5, $test_name5);
		
		// $test_name6 = 'Test lihat_video_baru()';
		// $test6 = $this->lihat_video_baru(63);
		// $expected_result6 = $test6 = true ;
		// $this->unit->run($test6, $expected_result6, $test_name6);
		
		// $test_name7 = 'Test validasi()';
		// $test7 = $this->validasi(63);
		// $expected_result7 = $test7 = true ;
		// $this->unit->run($test7, $expected_result7, $test_name7);
		
		// // $test_name8 = 'Test delete_video()';
		// // $test8 = $this->delete_video(63);
		// // $expected_result8 = $test8 = true ;
		// // $this->unit->run($test8, $expected_result8, $test_name8);
		
		// $test_name9 = 'Test data_video()';
		// $test9 = $this->data_video();
		// $expected_result9 = $test9 == NULL || $test9 > 0 ;
		// $this->unit->run($test9, $expected_result9, $test_name9);
		
		// echo $this->unit->report();
		
		
	// }
	
	// public function ubah_video()
	// {
		// $this->form_validation->set_rules('namaakun', 'Nama Akun', 'trim|required|alpha_dash|min_length[4]|xss_clean');
		// $this->form_validation->set_rules('email', 'Alamat Email', 'trim|required|valid_email');
		
		// if($this->form_validation->run() == FALSE)
		// {
			// $this->index();
		// }
		// else
		// {	
			// $username = $this->session->userdata('username');
			// $id_user = $this->session->userdata('id_user');
			// $data = array(
						// 'username' => $this->input->post('namaakun'),
						// 'nama_lengkap' => $this->input->post('namalengkap'),
						// 'jk' => $this->input->post('jk'),
						// 'tgl_lahir' => $this->input->post('tanggallahir'),
						// 'email' => $this->input->post('email'),
						// 'bidang_studi' => $this->input->post('bidang_studi'),
						// 'pendidikan' => $this->input->post('pendidikan'),
						// 'pekerjaan' => $this->input->post('pekerjaan')
					// );
			// $this->akun_model->edit_akun($id_user, $data);				
			// redirect('index.php/akun');
		// }
	// }
	
	
	// public function cari_video_kategori($kategori)
	// {
		// $this->video_model->get_video_per_kategori($kategori);
	// }
	
	// public function cari_video_daerah($daerah)
	// {
		// $this->video_model->get_video_per_daerah($daerah);
	// }
	
	
}
